package de.medizin.uni.halle.irm;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragment;

public class MailSettingsFragment extends SherlockFragment{	
	SQLiteDatabase database;

	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    	
    	final View v = inflater.inflate(R.layout.mailsettings, container, false);
    	database = getSherlockActivity().openOrCreateDatabase("born", Context.MODE_PRIVATE, null);    	
		Cursor c = database.rawQuery("SELECT * FROM settings", null);
		c.moveToFirst();
		Toast.makeText(getSherlockActivity(), String.valueOf(c.getCount()), Toast.LENGTH_LONG).show();
		
    	c.moveToPosition(4);
    	((EditText) v.findViewById(R.id.emailaddress)).setText(c.getString(c.getColumnIndex("parameter")));
    	c.moveToNext();
    	((EditText) v.findViewById(R.id.username)).setText(c.getString(c.getColumnIndex("parameter")));
    	c.moveToNext();
    	((EditText) v.findViewById(R.id.emailpassword)).setText(c.getString(c.getColumnIndex("parameter")));
    	c.moveToNext();
    	((EditText) v.findViewById(R.id.receivemailserver)).setText(c.getString(c.getColumnIndex("parameter")));
    	c.moveToNext();
    	((EditText) v.findViewById(R.id.receivemailport)).setText(c.getString(c.getColumnIndex("parameter")));
    	c.moveToNext(); c.moveToNext();
    	((EditText) v.findViewById(R.id.sendmailserver)).setText(c.getString(c.getColumnIndex("parameter")));
    	c.moveToNext();
    	((EditText) v.findViewById(R.id.sendmailport)).setText(c.getString(c.getColumnIndex("parameter")));
    	Spinner protocoll = (Spinner) v.findViewById(R.id.protocoll);
    	if(c.getString(c.getColumnIndex("parameter")).equals("imap"))
    		protocoll.setSelection(0);
    	else
    		protocoll.setSelection(1);
    	((Button) v.findViewById(R.id.save)).setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				ContentValues settings = new ContentValues();
				settings.put("parameter",((EditText) v.findViewById(R.id.emailaddress)).getText().toString());
				database.update("settings", settings, "setting = 'emailaddress'", null);
				settings.clear();
				settings.put("parameter",((EditText) v.findViewById(R.id.username)).getText().toString());	
				database.update("settings", settings, "setting = 'emailusername'", null);
				settings.clear();
				settings.put("parameter",((EditText) v.findViewById(R.id.emailpassword)).getText().toString());	
				database.update("settings", settings, "setting = 'emailpassword'", null);
				settings.clear();
				settings.put("parameter",((EditText) v.findViewById(R.id.receivemailserver)).getText().toString());	
				database.update("settings", settings, "setting = 'receiveserver'", null);
				settings.clear();
				settings.put("parameter",((EditText) v.findViewById(R.id.receivemailport)).getText().toString());	
				database.update("settings", settings, "setting = 'receiveport'", null);
				settings.clear();
				settings.put("parameter",((EditText) v.findViewById(R.id.sendmailserver)).getText().toString());	
				database.update("settings", settings, "setting = 'sendserver'", null);
				settings.clear();
				settings.put("parameter",((EditText) v.findViewById(R.id.sendmailport)).getText().toString());	
				database.update("settings", settings, "setting = 'sendport'", null);
				settings.clear();
				settings.put("parameter",((EditText) v.findViewById(R.id.protocoll)).getText().toString());	
				database.update("settings", settings, "setting = 'protocoll'", null);

	    		Toast.makeText(getSherlockActivity(),"Einstellungen gespeichert", Toast.LENGTH_SHORT).show();

			}
		});    	
        return v;
    }
}
